<template>
	<view>
		<view class="tips">
			<view class="title" style="font-size: 60rpx;">欢迎{{ isLogin? '登录' : '注册' }}{{ title }}</view>
		</view>
		<u-form :model="userInfo">
			<view class="container-input" style="margin-top: 40rpx;">
				<view class="eye-input" style="padding: 10px 0;">
					<input placeholder="请输入账号" v-model="userInfo.username" placeholderStyle="color: #c0c4cc"
						style="height: 25px; padding-right: 64rpx; width: 100%;"></input>

				</view>
			</view>
			<view class="container-input" style="margin-top: 40rpx;">
				<view class="eye-input" style="padding: 10px 0;">
					<input :password="pwdOpen" placeholder="请输入密码" v-model="userInfo.password"
						placeholderStyle="color: #c0c4cc"
						style="height: 25px; padding-right: 64rpx; width: 100%;"></input>
				</view>
			</view>

			<view v-show="!isLogin" class="container-input" style="margin-top: 40rpx;">
				<view class="eye-input" style="padding: 10px 0;">
					<input type="tel" placeholder="请输入手机号" v-model="userInfo.phone" placeholderStyle="color: #c0c4cc"
						style="height: 25px; padding-right: 64rpx; width: 100%;"></input>
				</view>
			</view>
			<view v-show="!isLogin" class="container-input" style="margin-top: 40rpx;">
				<view class="eye-input" style="padding: 10px 0;">
					<input type="text" placeholder="请输入邮箱" v-model="userInfo.email" placeholderStyle="color: #c0c4cc"
						style="height: 25px; padding-right: 64rpx; width: 100%;"></input>
				</view>
			</view>

		</u-form>


		<view class="button">
			<view v-if="isLogin" class="container-bth" @click="login">登录</view>
			<view v-if="!isLogin" class="container-bth" @click="register">注册</view>
		</view>

		<view class="flex-between" :style="{ justifyContent: isLogin? '' : 'flex-end' }"
			style="margin: 32rpx 40rpx 0 40rpx; display: flex;s">
			<view class="forget-pwd" @click="checkBtn">
				{{!isLogin? '立即登录?' : '立即注册?' }}
			</view>
			<view v-if="isLogin" class="forget-pwd" @click="forget">忘记密码?</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue';
	import $req from './request';
	// 定义响应式数据
	const isLogin = ref(true)
	const title = ref('');
	const eyeOpen = ref(false);
	const pwdOpen = ref(true);
	const userInfo = reactive({
		username: '',
		password: '',
		phone: '',
		email: ''

	});
	// 登录方法
	const login = async () => {
		if (userInfo.username == '') {
			uni.showToast({
				title:'请输入用户名'
			})
		} else if (userInfo.password == '') {
			uni.showToast({
				title:'请输入密码'
			})
		} else {
			const {user} = await $req.login(userInfo.username, userInfo.password);
			if (user == void[0]) {
				uni.showToast({
					title: '登陆失败'
				});
				userInfo.username = ''
				userInfo.password = ''
				return
			}
			console.log(user);
			uni.setStorageSync("user",user)
			uni.switchTab({
				url: '/pages/index/index'
			});
		}
	};

	// 注册方法
	const register = async () => {
		if (userInfo.username == '') {
			uni.showToast({
				title:'请输入用户名'
			})
		} else if (userInfo.password == '') {
			uni.showToast({
				title:'请输入密码'
			})
		} else {
			const res = await $req.register(userInfo.username, userInfo.password, userInfo.phone, userInfo.email)
			userInfo.password = '';
			userInfo.username = '';
			if(res.code !== 201) return
			isLogin.value = true
		}
	};

	// 切换状态方法
	const checkBtn = () => {
		isLogin.value = !isLogin.value;
		userInfo.username = '';
		userInfo.password = '';
		eyeOpen.value = false;
		pwdOpen.value = true;
	};

	// 隐藏密码方法
	const tabEyeClose = () => {
		eyeOpen.value = true;
		pwdOpen.value = false;
	};

	// 显示密码方法
	const tabEyeOpen = () => {
		eyeOpen.value = false;
		pwdOpen.value = true;
	};

	// 忘记密码方法
	const forget = () => {
		uni.navigateTo({
			url: `/pagesHome/editPassword/editPassword`
		});
	};
</script>

<style>
	page {
		background: linear-gradient(270deg, #E1ECFE 0%, #DDFCFF 100%);
	}
</style>

<style lang="scss" scoped>
	.tips {
		margin: 110rpx 0 100rpx 0;
		width: 100%;
		text-align: center;

		.title {
			font-size: 40rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			letter-spacing: 2rpx;
			color: #26344D;
		}

		.sub-title {
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			letter-spacing: 2rpx;
			color: #26344D;
			margin-top: 10rpx;
		}
	}

	.container-input {
		margin: 0 40rpx;
		background-color: #fff;
		borderRadius: 16rpx;
		paddingLeft: 32rpx;
		paddingRight: 32rpx;

		.eye-input {
			width: 100%;
			position: relative;
			display: flex;
			justifyContent: space-between;
			alignItems: center;

			.eye-image {
				position: absolute;
				right: 0;
				width: 30rpx;
				height: 30rpx;
				marginLeft: 20rpx;
				flex-shrink: 0;

				img {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	.agree-list {
		margin: 16px 0 0 20px;
		display: flex;

		.agree-radio {
			width: 26rpx;
			height: 26rpx;
			borderRadius: 50%;
			border: 1px solid #999;
		}

		.agree-radio-active {
			position: relative;
			width: 26rpx;
			height: 26rpx;
			borderRadius: 50%;
			border: 1px solid transparent;
			backgroundColor: #428AF6;
		}

		.agree-radio-active::after {
			position: absolute;
			content: '';
			left: 8rpx;
			width: 6rpx;
			height: 14rpx;
			top: 50%;
			transform: translateY(-60%) rotate(38deg);
			border: 4rpx solid #fff;
			borderWidth: 0 4rpx 4rpx 0;
		}

		.agree-title {
			marginLeft: 10rpx;
			font-size: 26rpx;
			color: #333;
		}
	}

	.button {
		display: flex;
		justify-content: center;
		align-items: center;
		margin: 10px;
		padding: 10px;
	}

	// 登录和注册按钮的通用样式
	.container-bth {


		margin: 10px;
		padding: 10px;
		// 背景颜色
		background-color: #007BFF;
		// 文字颜色
		color: white;
		// 按钮高度
		height: 80%;
		// 按钮宽度
		width: 100%;
		// 按钮边框圆角
		border-radius: 5px;
		// 文字水平居中
		text-align: center;
		// 文字垂直居中
		line-height: 50px;

		// 鼠标悬停时的样式变化
		&:hover {
			background-color: #0056b3;
		}
	}

	.forget-pwd {
		display: flex;
		alignItems: center;
		justifyContent: flex-end;
		fontSize: 28rpx;
		fontFamily: MiSans-Regular, MiSans;
		color: #5990F5;
	}
</style>